#!/usr/bin/perl -w
use XML::XPath;
use XML::XPath::XMLParser;

use strict; use warnings; use diagnostics;
print "Content-type:text/html\r\n\r\n";
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
my $buffer;
if ($ENV{'REQUEST_METHOD'} eq "POST"){
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
    $buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
my @pairs = split(/&/, $buffer); #array
my $name;
my $value; #var
my %FORM; #hash
foreach my $pair (@pairs){
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}
$name = $FORM{name};

print "<html>";
print "<head>";
print "<title>Hello</title>";
print "</head>";
print "<body>";
my $xp = XML::XPath->new(filename => 'prova.xml');
my $nodeset = $xp->find("//utente[name='$value']");
if($nodeset->size()==1){
	print "Utente loggato";
	print "<h2>Hello $name </h2>";
}
else{
	print "<h2>Utente $name non esistente</h2>";
}
print "</body>";
print "</html>";
